<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row type="flex">
          <a-col>
            <a-button icon="plus" style="margin-right: 20px" type="primary" @click="openAdd()">新增</a-button>
            <a-divider style="margin-right: 20px" type="vertical" />
          </a-col>
          <a-col :md="4" :sm="6">
            <a-form-item label="指标">
              <j-input v-model="queryParam.code" placeholder="请输入指标名称"></j-input>
            </a-form-item>
          </a-col>
          <a-col flex="auto">
            <span class="table-page-search-submitButtons" style="float: left; overflow: hidden; margin-right: 20px">
              <!--              <a-button icon="search" type="primary" @click="searchQuery">查询</a-button>-->
              <a-popover :overlayStyle="{ background: 'white' }" :visible="tooltipShow" placement="topLeft">
                <template slot="content">
                  <a-icon theme="twoTone" two-tone-color="#52c41a" type="exclamation-circle" /><span
                    style="margin-left: 5px"
                    >可以点击重置来重新筛选结果</span
                  >
                </template>
                <a-button icon="search" type="primary" @click="searchQuery" style="margin-left: 20px">查询</a-button>
                <a-button icon="reload" style="margin-left: 8px" @click="searchReset">重置</a-button>
              </a-popover>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div>
      <a-table
        ref="table"
        :columns="columns"
        :dataSource="dataSource"
        :loading="loading"
        :pagination="ipagination"
        bordered
        class="Atable"
        rowKey="id"
        size="middle"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record">
          <a-button style="margin-right: 10px" type="primary" @click="handleEdit(record)"> 编辑 </a-button>
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a-button> 删除 </a-button>
          </a-popconfirm>
        </span>
        <span slot="type" slot-scope="text, record">
          <a-tag v-if="text == 'GUO'" color="red">国标</a-tag>
          <a-tag v-if="text == 'HANG'" color="blue">行标</a-tag>
          <a-tag v-else-if="text == 'DI'" color="green">地标</a-tag>
        </span>
      </a-table>
      <more-table-modal ref="modalForm" @ok="modalFormOk"></more-table-modal>
    </div>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import JInput from '@comp/jeecg/JInput'
import MoreTableModal from '@views/dataVisualization/components/moreTableModal'

export default {
  name: 'moreTable',
  mixins: [JeecgListMixin],
  components: {
    MoreTableModal,
    JInput,
  },
  data() {
    return {
      tooltipShow: false,
      columns: [],
      url: {
        list: '/carbon/targetTrackeList/list',
        delete: '/carbon/targetTrackeList/delete',
      },
    }
  },
  watch: {
    // 监听 dataSource 的变化
    dataSource: {
      handler(newVal, oldVal) {
        // 当 dataSource 更新时调用 getYearList 方法
        this.getYearList()
      },
      deep: true, // 设置为深度监听，确保当 dataSource 对象内部的属性变化时也能触发
    },
  },
  methods: {
    openAdd() {
      this.$refs.modalForm.add()
      this.$refs.modalForm.title = '新增'
      this.$refs.modalForm.disableSubmit = false
    },
    getYearList() {
      this.loading = true
      setTimeout(() => {
        this.columns = [
          {
            title: '指标',
            dataIndex: 'indexStr',
            align: 'center',
            // scopedSlots: { customRender: 'name' },
            // width:420,
          },
          {
            title: '单位',
            dataIndex: 'unit',
            align: 'center',
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            width: 200,
            scopedSlots: { customRender: 'action' },
          },
        ]
        let currentYearList = this.result.currentYearList
        let targetList = this.result.targetList

        localStorage.setItem('currentYearList', JSON.stringify(currentYearList))
        localStorage.setItem('targetList', JSON.stringify(targetList))

        let current = {
          title: '现状',
          align: 'center',
          children: [],
        }
        let target = {
          title: '目标',
          align: 'center',
          children: [],
        }

        let children1 = []
        let children2 = []

        currentYearList.forEach((item) => {
          children1.push({
            title: item,
            dataIndex: item,
            align: 'center',
          })
        })
        // 按照年份排序
        children1.sort((a, b) => {
          return a.title - b.title
        })
        current.children = children1

        targetList.forEach((item) => {
          children2.push({
            title: item,
            dataIndex: item,
            align: 'center',
          })
        })
        // 按照年份排序
        children2.sort((a, b) => {
          return a.title - b.title
        })
        target.children = children2

        // 插入到columns[2]后面
        this.columns.splice(2, 0, current, target)
        this.loading = false
        this.setData()
      }, 200)
    },
    setData() {
      for (let i in this.dataSource) {
        //   将target转为对象，并且拼入this.dataSource[i]
        let target = {}
        target = JSON.parse(this.dataSource[i].target)
        this.dataSource[i] = Object.assign(this.dataSource[i], target)
      }
    },
  },
  mounted() {
    this.getYearList()
  },
}
</script>

<style scoped></style>
